package com.wzy.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wzy.server.mapper.MenuRoleMapper;
import com.wzy.server.pojo.MenuRole;

import com.wzy.server.pojo.RespBean;
import com.wzy.server.service.IMenuRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wzy
 * @since 2021-04-30
 */
@Service
public class MenuRoleServiceImpl extends ServiceImpl<MenuRoleMapper, MenuRole> implements IMenuRoleService {

    @Autowired
    private MenuRoleMapper menuRoleMapper;

    //    更新角色菜单
    @Override
    @Transactional   //更新菜单时，先删除菜单，再重新关联菜单
    public RespBean updateMenuRole(Integer rid, Integer[] mids) {
//                             删除角色下的菜单id
        menuRoleMapper.delete(new QueryWrapper<MenuRole>().eq("rid",rid));
//        菜单id为空表示删除，更新成功
        if (null==mids||0==mids.length){
            return RespBean.success("更新成功");
        }
//        批量更新
        Integer result = menuRoleMapper.insertRecord(rid, mids);
//        如果返回长度与mids相同
        if (result==mids.length){
            return RespBean.success("更新成功");
        }
        return RespBean.error("更新失败");
    }
}
